package com.atguigu.tingshu.user.receiver;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @className: UserReceiver
 * @author: xiaoxu
 * @date: 2024/9/2 10:36
 * @Version: 1.0
 * @description:
 */
@Component
@Slf4j
public class UserReceiver {


    @Autowired
    private UserInfoService userInfoService;

    /**
     * 处理用户购买记录；VIP会员、专辑、声音
     * @param record 购买记录的字符符串
     *               {"itemIdList"
     *               :[20760,20761,20762,20763,20764,20765,
     *               20766,20767,20768,20769,20770,20771,20772,20773,20774,
     *               20775,20776,20777,20778,20779,20780,20781,20782,20783,
     *               20784,20785,20786,20787,20788,20789,20790,20791,20792,
     *               20793,20794,20795,20796,20797,20798,20799],
     *               "itemType":"1002",
     *               "orderNo":"202410031841774421707243520",
     *               "userId":20}
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_PAY_RECORD)
    public void saveUserPayRecord(ConsumerRecord<String, String> record) {
        String userPaidRecordStr = record.value();
        if (StringUtils.isNotBlank(userPaidRecordStr)) {
            log.info("[用户服务]监听处理购买记录：{}", userPaidRecordStr);
            UserPaidRecordVo userPaidRecordVo = JSON.parseObject(userPaidRecordStr, UserPaidRecordVo.class);
            userInfoService.saveUserPayRecord(userPaidRecordVo);
        }
    }

}

